import maya.cmds as mc
class creatJntMuscle():
    def __init__(self):
        pass
    def creatJntMuscleWin(self):
        creatJntMuscleWin = 'creatJntMuscleWin'
        if mc.window(creatJntMuscleWin,ex = True):
            mc.deleteUI(creatJntMuscleWin)
        
        mc.window(creatJntMuscleWin,widthHeight=(300, 200),t='creatJntMuscleWin v1.01',menuBar = True,rtf=True,s=True)
        
        mc.rowColumnLayout('Main',numberOfColumns=1,w=300,cal = (5,'center'),m=True)
        mc.rowColumnLayout('nameFieldLay',numberOfColumns=2,w=300,cal = (5,'center'),m=True)
        mc.text('settingBasenameText',l='setting name')
        mc.textField('settingBasename')
        mc.setParent('..')
        mc.button('createTempleLocator',w=300,h=30,c=lambda *args:self.createLoc())
        
        mc.button('createSetting',w=300,h=30,c=lambda *args:self.createSettings())
        mc.radioButtonGrp('mirrorSection',numberOfRadioButtons=2, label='', labelArray2=['selection', 'textField'],cw3=[30,120,100],select=1)

        mc.button('Mirrot Setting',w=300,h=30,c=lambda *args:self.mirrorSettings())
        
        #mc.radioButtonGrp('mirrorSection',e=True,cw2=[100,150],co2=[-50,-50] )
        
        mc.text('settingBasenameText',e=True,w=90)
        mc.textField('settingBasename',e=True,w=200)
        mc.showWindow()
    def createTempleLocator(self,baseLoc,TipLoc):
        mc.select(cl=True)
        mc.spaceLocator(n=baseLoc)
        mc.move(-5,0,0,baseLoc)
        mc.select(cl=True)
        mc.spaceLocator(n=TipLoc)
        mc.move(5,0,0,TipLoc)
    def createSetting(self,baseLoc,TipLoc,settingbase):
        jntBase=str(baseLoc).replace('_poloc','_jnt')
        jntBaseEnd=str(baseLoc).replace('_poloc','_jntend')
        jntTip=str(TipLoc).replace('_poloc','_jnt')
        jntTipEnd=str(TipLoc).replace('_poloc','_jntend')
        
        basePos=mc.getAttr(str(baseLoc)+'.translate')
        TipPos=mc.getAttr(str(TipLoc)+'.translate')
        mc.select(cl=True)
        mc.joint(n=jntBase,p=basePos[0])
        mc.joint(n=jntBaseEnd,p=TipPos[0])
        mc.joint(jntBase,e=True,oj='xyz')
        
        mc.select(cl=True)
        mc.joint(n=jntTip,p=TipPos[0])
        mc.joint(n=jntTipEnd,p=basePos[0])
        mc.joint(jntTip,e=True,oj='xyz')
        
        ikHandleBase=str(baseLoc).replace('_poloc','_ikHandle')
        ikHandleTip=str(TipLoc).replace('_poloc','_ikHandle')
        mc.ikHandle(n=ikHandleBase,sj=jntBase,ee=jntBaseEnd,sol='ikSCsolver')

        mc.ikHandle(n=ikHandleTip,sj=jntTip,ee=jntTipEnd,sol='ikSCsolver')
        
        
        basePocon=str(ikHandleBase)+'_pocon'
        TipPocon=str(ikHandleTip)+'_pocon'
        followBase=str(ikHandleTip)+'_followBase'

        mc.group(em=True,n=basePocon)
        mc.pointConstraint(ikHandleBase,basePocon,n='TempleCon')
        mc.delete('TempleCon')
        mc.pointConstraint(basePocon,ikHandleBase)
        mc.group(em=True,n=TipPocon)
        mc.pointConstraint(ikHandleTip,TipPocon,n='TempleCon')
        mc.delete('TempleCon')
        mc.pointConstraint(TipPocon,ikHandleTip)
        
        mc.group(em=True,n=followBase)
        mc.pointConstraint(ikHandleTip,followBase,n='TempleCon')
        mc.delete('TempleCon')
        
        dimension01=str(settingbase)+'_01_dimension'
        dimension02=str(settingbase)+'_02_dimension'
        loc01=str(settingbase)+'_01_dis_loc'
        loc02=str(settingbase)+'_02_dis_loc'
        loc03=str(settingbase)+'_03_dis_loc'
        
        print(basePos[0])
        
        
        dimension01proShape=mc.distanceDimension(sp=basePos[0],ep=TipPos[0])
        dimension02proShape=mc.distanceDimension(sp=basePos[0],ep=(0,0.12512,0))
        dimension01pro=mc.listRelatives(dimension01proShape,p=True)
        dimension02pro=mc.listRelatives(dimension02proShape,p=True)
        startloc=mc.listConnections(str(dimension01proShape)+'.startPoint',s=True)
        endloc01=mc.listConnections(str(dimension01proShape)+'.endPoint',s=True)
        endloc02=mc.listConnections(str(dimension02proShape)+'.endPoint',s=True)
        
        
        mc.rename(startloc,loc01)
        mc.rename(endloc01,loc02)
        mc.rename(endloc02,loc03)
        mc.rename(dimension01pro,dimension01)
        mc.rename(dimension02pro,dimension02)
        print(loc01,loc02,loc03)
        mc.setAttr(str(loc03)+'.translateX',TipPos[0][0])
        mc.setAttr(str(loc03)+'.translateY',TipPos[0][1])
        mc.setAttr(str(loc03)+'.translateZ',TipPos[0][2])
        
        grpbase=str(baseLoc).replace('_poloc','_grp')
        grpTip=str(TipLoc).replace('_poloc','_grp')
        mc.group(em=True,n=grpbase)
        mc.pointConstraint(loc01,grpbase,n='TempleCon')
        mc.delete('TempleCon')
        mc.group(em=True,n=grpTip)
        mc.pointConstraint(ikHandleBase,grpTip,n='TempleCon',mo=False)
        mc.delete('TempleCon')
        
        mc.parent(ikHandleBase,jntBase,TipPocon,loc01,loc02,grpbase)
        mc.parent(ikHandleTip,jntTip,basePocon,loc03,grpTip)
        
        baseMd=str(baseLoc).replace('_poloc','_md')
        TipMd=str(TipLoc).replace('_poloc','_md')
        
        mc.createNode('multiplyDivide',n=baseMd)
        mc.createNode('multiplyDivide',n=TipMd)
        mc.setAttr(str(baseMd)+'.operation',2)
        mc.setAttr(str(TipMd)+'.operation',2)

        dimenShape01=mc.listRelatives(dimension01,s=True)[0]
        dimenShape02=mc.listRelatives(dimension02,s=True)[0]
        mc.connectAttr(str(dimenShape02)+'.distance',str(baseMd)+'.input1X')
        mc.connectAttr(str(dimenShape01)+'.distance',str(baseMd)+'.input2X')
        mc.connectAttr(str(baseMd)+'.output.outputX',str(jntBase)+'.scaleX')
        mc.connectAttr(str(baseMd)+'.output.outputX',str(jntTip)+'.scaleX')
        
    def createLoc(self):
        baseName=mc.textField('settingBasename',q=True,tx=True)
        baseLoc=baseName+'_bottom_poloc'
        TipLoc=baseName+'_Tip_poloc'
        self.createTempleLocator(baseLoc,TipLoc)
        
    def createSettings(self):
        baseName=mc.textField('settingBasename',q=True,tx=True)
        baseLoc=baseName+'_bottom_poloc'
        TipLoc=baseName+'_Tip_poloc'
        self.createSetting(baseLoc,TipLoc,baseName)
    def mirrorSettings(self):
        section=mc.radioButtonGrp('mirrorSection',q=True,select=True)
        if section == 2:
            baseName=mc.textField('settingBasename',q=True,tx=True)
            if 'L_' in baseName:
                newbaseName=baseName.replace('L_','R_')
            else:
                mc.error('sorry, currently ,it is worked only "L_" in textField')
        else:
            selectObj=mc.ls(sl=True)
            for i in selectObj:
                if 'L_' in str(i):
                    splitOb=str(i).split('_')
                    if splitOb[-1]=='loc' or splitOb[-1]=='pocon':
                        
                        mount=len(splitOb[0:-3])
                        for k in rang(mount):
                            if k==0:
                                baseName=splitOb[k]
                            else:
                                baseName=baseName+'_'+splitOb[k]
                    if splitOb[-1]=='jnt' or splitOb[-1]=='grp' or splitOb[-1]=='ikHandle' or splitOb[-1]=='jntend':
                        mount=len(splitOb[0:-2])
                        for k in range(mount):
                            if k==0:
                                baseName=splitOb[k]
                            else:
                                baseName=baseName+'_'+splitOb[k]
                    newbaseName=baseName.replace('L_','R_')
                else:
                    mc.error('sorry, currently ,it is worked only "L_" in textField')
        baseLoc=newbaseName+'_bottom_poloc'
        TipLoc=newbaseName+'_Tip_poloc'
        
        LBaseGrp=baseName+'_bottom_grp'
        RBaseLoc=newbaseName+'_bottom_poloc'
        
        LTipGrp=baseName+'_Tip_grp'
        RTipLoc=newbaseName+'_Tip_poloc'
        Bpos=mc.xform(LBaseGrp,ws=True,q=True,t=True)
        Tpos=mc.xform(LTipGrp,ws=True,q=True,t=True)
        self.createTempleLocator(baseLoc,TipLoc)
        mc.xform(RBaseLoc,ws=True,t=(-Bpos[0],Bpos[1],Bpos[2]))
        mc.xform(RTipLoc,ws=True,t=(-Tpos[0],Tpos[1],Tpos[2]))
        
        self.createSetting(baseLoc,TipLoc,newbaseName)
        
        
        